package ods_table

import (
	"server/global"
	"server/model/common/request"
)

type OdsDbTableService struct {
}

func (odsDbTableService *OdsDbTableService) CreateOdsDbTable(odsDbTable *OdsDbTable) (err error) {
	err = global.SysDB.Create(odsDbTable).Error
	return err
}

func (odsDbTableService *OdsDbTableService) DeleteOdsDbTable(odsDbTable OdsDbTable) (err error) {
	err = global.SysDB.Delete(&odsDbTable).Error
	return err
}

func (odsDbTableService *OdsDbTableService) DeleteOdsDbTableByIds(ids request.IdsReq) (err error) {
	err = global.SysDB.Delete(&[]OdsDbTable{}, "id in ?", ids.Ids).Error
	return err
}

func (odsDbTableService *OdsDbTableService) UpdateOdsDbTable(odsDbTable OdsDbTable) (err error) {
	err = global.SysDB.Save(&odsDbTable).Error
	return err
}

func (odsDbTableService *OdsDbTableService) GetOdsDbTableById(id uint) (odsDbTable OdsDbTable, err error) {
	err = global.SysDB.Where("id = ?", id).First(&odsDbTable).Error
	return
}

func (odsDbTableService *OdsDbTableService) GetOdsDbTableInfoList(info OdsDbTableSearch) (list []OdsDbTable, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.SysDB.Model(&OdsDbTable{})
	var odsDbTables []OdsDbTable
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	err = db.Limit(limit).Offset(offset).Find(&odsDbTables).Error
	return odsDbTables, total, err
}

func (odsDbTableService *OdsDbTableService) GetAllOdsDbTables() (odsDbTable []OdsDbTable, err error) {
	err = global.SysDB.Find(&odsDbTable).Error
	return odsDbTable, err
}
